package com.sangeng.controller;

import com.sangeng.domain.dto.AssignRolePermissionsDTO;
import com.sangeng.domain.vo.PermissionTreeVO;
import com.sangeng.domain.vo.Result;
import com.sangeng.service.IPermissionRoleService;
import com.sangeng.service.IPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/system")
@Slf4j
public class PermissionController {
    
    @Resource
    private IPermissionService permissionService;
    @Resource
    private IPermissionRoleService permissionRoleService;
    
    /**
     * 获取权限树
     */
    @GetMapping("/permission/tree")
    public Result<List<PermissionTreeVO>> getPermissionTree() {
        log.info("获取权限树");
        List<PermissionTreeVO> tree = permissionService.getPermissionTree();
        return Result.ok(tree);
    }

    /**
     * 获取角色权限
     */
    @GetMapping("/role/permissions/{roleId}")
    public Result<List<Long>> getRolePermission(@PathVariable("roleId") Long roleId) {
        log.info("获取角色权限：{}",roleId);
        List<Long> permissionIds = permissionRoleService.getRolePermission(roleId);
        return Result.ok(permissionIds);
    }

    /**
     * 分配角色权限
     */
    @PostMapping("/role/assignPermissions")
    public Result assignRolePermission(@RequestBody AssignRolePermissionsDTO assignRolePermissionsDTO) {
        log.info("分配角色权限：{}",assignRolePermissionsDTO);
        permissionRoleService.assignRolePermission(assignRolePermissionsDTO);
        return Result.ok("分配成功");
    }
}
